﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Security.AccessControl;

namespace Simply.BusinessTier.FileStorage
{
    internal class FileStoreService
    {
        private static string storagePath = ConfigurationSettings.AppSettings["fileStorage"];
        private static string filePathPattern = ConfigurationSettings.AppSettings["folderPattern"];
        private static string thumbFilePath = ConfigurationSettings.AppSettings["thumbFileName"];
        private static string editFilePath = ConfigurationSettings.AppSettings["editFileName"];
        private static string printFilePath = ConfigurationSettings.AppSettings["printFileName"];
        private static string layoutFilePath = ConfigurationSettings.AppSettings["layoutFilePath"];
        private static string fontFilePath = ConfigurationSettings.AppSettings["fontFilePath"];

        private static string packageDirPath = ConfigurationSettings.AppSettings["packageDirPath"];
        private static string pdfFileName = ConfigurationSettings.AppSettings["pdfFileName"];
        private static string previewFileName = ConfigurationSettings.AppSettings["previewFileName"];

        /*public Stream GetThumbImageStream(long productId, string type)
        {
            RegexStringValidator val = new RegexStringValidator("//A-Z");
            string path = storagePath + filePathPattern + thumbFilePath;
            path = String.Format(path, type, productId);
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
            return fs;
        }

        public Stream GetPreviewImageStream(long productId, string type)
        {
            RegexStringValidator val = new RegexStringValidator("//A-Z");
            string path = storagePath + filePathPattern + thumbFilePath;
            path = String.Format(path, type, productId);
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
            return fs;
        }

        public Stream GetEditImageStream(long productId, string type)
        {
            RegexStringValidator val = new RegexStringValidator("//A-Z");
            string path = storagePath + filePathPattern + editFilePath;
            path = String.Format(path, type, productId);
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
            return fs;
        }

        public Stream GetLayoutImageStream(long layoutId, string type)
        {
            RegexStringValidator val = new RegexStringValidator("//A-Z");
            string path = storagePath + layoutFilePath;
            path = String.Format(path, type, layoutId);
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
            return fs;
        }*/

        

        public static string GetFontFilePath(string name, string fileName)
        {
            string path = storagePath + fontFilePath;
            return String.Format(path, name, fileName);
        }

        public static string GetPrintFilePath(string type, long productId)
        {
            string path = storagePath + filePathPattern + printFilePath;
            return String.Format(path, type, productId);
        }

        public static string GetPDFStoreFilePath(long packageId, string productType)
        {
            string dirPath = storagePath + packageDirPath;
            dirPath = String.Format(dirPath, productType, DateTime.Now.ToString("MM_yy"), packageId);

            DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }
            return dirPath + pdfFileName;
          
        }

        public static string GetPDFStoreDirPath(string pdfFilePath)
        {
            return pdfFilePath.Remove(pdfFilePath.Length - pdfFileName.Length);
        }
    }
}
